<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <!-- 避免IE使用兼容模式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="renderer" content="webkit">
    <!-- TopJUI框架样式 -->
    <link type="text/css" href="/static/topjui/themes/default/topjui.core.min.css" rel="stylesheet">
    <link type="text/css" href="/static/topjui/themes/default/topjui.blue.css" rel="stylesheet" id="dynamicTheme"/>
    <!-- FontAwesome字体图标 -->
    <link type="text/css" href="/static/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>
    <!-- layui框架样式 -->
    <link type="text/css" href="/static/plugins/layui/css/layui.css" rel="stylesheet"/>
    <!-- jQuery相关引用 -->
    <script type="text/javascript" src="/static/plugins/jquery/jquery.min.js"></script>
    <script type="text/javascript" src="/static/plugins/jquery/jquery.cookie.js"></script>
    <!-- TopJUI框架配置 -->
    <script type="text/javascript" src="/static/public/js/topjui.config.js"></script>
    <!-- TopJUI框架核心-->
    <script type="text/javascript" src="/static/topjui/js/topjui.core.min.js"></script>
    <!-- TopJUI中文支持 -->
    <script type="text/javascript" src="/static/topjui/js/locale/topjui.lang.zh_CN.js"></script>
    <!-- layui框架js -->
    <script type="text/javascript" src="/static/plugins/layui/layui.js" charset="utf-8"></script>
</head>

<body>
<!-- datagrid表格 -->
<table data-toggle="topjui-datagrid"
       data-options="id: 'sBchDg',
                fitColumns:true,
                remoteSort:false,
                url: '/sBch/data',
                pageSize: 10">
    <thead>
    <tr>
        <th data-options="field:'uuid',title:'UUID',checkbox:true"></th>
        <th data-options="field:'bchCde',title:'机构代码',sortable:true,width:100"></th>
        <th data-options="field:'bchName',title:'机构名称',sortable:true,width:100"></th>
        <th data-options="field:'bchContUsr',title:'机构联系人姓名',sortable:true,width:50"></th>
        <th data-options="field:'bchContTel',title:'机构联系人电话',sortable:true,width:80"></th>
        <th data-options="field:'bchAddr',title:'机构地址',sortable:true,width:100"></th>
        <th data-options="field:'bchSts',title:'状态',sortable:true,width:50,hidden:true"></th>
        <th data-options="field:'bchStsText',title:'状态',sortable:true,width:50,formatter: function(value,row,index){
						if (value == '有效'){
							return '<span style=\'color:green\'>有效</span>';
						} else if (value == '无效') {
							return '<span style=\'color:red\'>无效</span>';
						} else {
						    return value;
						}
                    }"></th>
        <th data-options="field:'updateUser',title:'更新人',sortable:true,width:50"></th>
        <th data-options="field:'updateTime',title:'更新时间',sortable:true,width:100"></th>
        <th data-options="field:'operate',title:'操作',formatter:operateFormatter,width:80"></th>
    </tr>
    </thead>
</table>

<!-- 表格工具栏开始 -->
<div id="sBchDg-toolbar" class="topjui-toolbar"
     data-options="grid:{
           type:'datagrid',
           id:'sBchDg'
       }">
    <a href="javascript:void(0)"
       data-toggle="topjui-menubutton"
       data-options="method:'openDialog',
       extend: '#sBchDg-toolbar',
       iconCls: 'fa fa-plus',
       btnCls: 'topjui-btn-blue',
       dialog:{
           id:'sbchAddDialog',
           title:'新增/编辑',
           href:'/sBch/add',
           buttonsGroup:[
               {text:'保存',url:'/sBch/save?flag=add',iconCls:'fa fa-plus',handler:'ajaxForm',btnCls:'topjui-btn-green'}
           ]
       }">新 增</a>
    <a href="javascript:void(0)"
       data-toggle="topjui-menubutton"
       data-options="method: 'openDialog',
            extend: '#sBchDg-toolbar',
            iconCls: 'fa fa-pencil',
            btnCls: 'topjui-btn-green',
            grid: {
                type: 'datagrid',
                id: 'sBchDg'
            },
            dialog: {
                title:'新增/编辑',
                href:'/sBch/edit',
                url: '/sBch/detail?bchCde={bchCde}',
                buttonsGroup: [
                    {
                        text: '更新',
                        url: '/sBch/save?flag=edit',
                        iconCls: 'fa fa-save',
                        handler: 'ajaxForm',
                        btnCls: 'topjui-btn-green'
                    }
                ]
            }">编 辑</a>
    <a href="javascript:void(0)"
       data-toggle="topjui-menubutton"
       data-options="method:'doAjax',
       extend: '#sBchDg-toolbar',
       btnCls:'topjui-btn-red',
       iconCls:'fa fa-trash',
       confirmMsg:'删除机构会导致系统不稳定，是否确定删除勾选内容？',
       grid: {
                uncheckedMsg:'请先勾选要删除的数据',
                param:'bchCde:bchCde',
                updateRow:false
              },
       url:'/sBch/delete'">删 除</a>
</div>
<!-- 表格工具栏结束 -->
<script>
    function operateFormatter(value, row, index) {
        var htmlstr = '<button class="layui-btn layui-btn-xs" onclick="openEditDiag(\'' + row.bchCde + '\')">编辑</button>';
        htmlstr += '<button class="layui-btn layui-btn-xs layui-btn-danger" onclick="deleteRow(\'' + row.bchCde + '\')">删除</button>';
        return htmlstr;
    }

    function openEditDiag(bchCde) {
        var $editDialog = $('<form  id="myDialog" method="get"></form>'); // 创建form表单元素
        $editDialog.iDialog({
            title: '编辑数据',
            width: 950,
            height: 500,
            closed: false,
            cache: false,
            href: '/sBch/edit?bchCde=' + bchCde,
            modal: true,
            buttons: [{
                text: '保存',
                iconCls: 'fa fa-save',
                btnCls: 'topjui-btn-blue',
                handler: ajaxSubmit // 调用下面自定义方法ajaxSubmit()
            }, {
                text: '关闭',
                iconCls: 'fa fa-close',
                btnCls: 'topjui-btn-red',
                handler: function () {
                    $editDialog.iDialog('close');
                }
            }],
            onLoad: function () {
                //加载表单数据
                $.getJSON('/sBch/detail?bchCde=' + bchCde, function (data) {
                    $editDialog.form('load', data);
                });
            }
        });
    }

    function ajaxSubmit() {
        if ($('#myDialog').form('validate')) {
            var formData = $("#myDialog").serialize();
            var url = '/sBch/save?flag=edit';
            $.post(url, formData, function (res) {
                if (res.statusCode == 200) { // 后台返回状态值
                    $.iMessager.show({title: '操作提示', msg: res.message, timeout: 5000, showType: 'slide'});// '消息窗口'组件
                    $("#sBchDg").iDatagrid('reload');// 刷新下表格数据
                    $('#myDialog').dialog('destroy');// 销毁dialog面板
                } else {
                    $.iMessager.show({title: '操作提示', msg: res.message, timeout: 5000, showType: 'slide'});// '消息窗口'组件
                }
            }, 'JSON')
        }
    }

    function deleteRow(bchCde) {
        $.iMessager.confirm('确认提示', '删除会导致系统不稳定，是否确定删除勾选内容？', function (r) {
            if (r) {
                var postData = {bchCde: bchCde}; // 请求中携带的JSON数据
                var url = '/sBch/delete';//请求的地址
                $.post(url, postData, function (res) {
                    $.iMessager.show({title: '操作提示', msg: res.message, timeout: 5000, showType: 'slide'});// '消息窗口'组件
                    if (res.statusCode == 200) {
                        $("#sBchDg").iDatagrid('reload');//请求成功后刷新表格
                    }
                }, "JSON")
            }
        });
        // $.iMessager.alert('操作提示', '请根据具体情况编写代码，如ajax删除请求，请求失败提示，请求成功提示，请求成功后刷新表格等！', 'messager-info');
    }

</script>
</body>
</html>
